package net.techwatch.guice.dao;

import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;

public class EntityManagerProvider implements Provider<EntityManager>{
	
	private Properties properties;
	
	@Inject
	private EntityManagerProvider(@Named("hibernate.connection.url") String url, 
			@Named("hibernate.connection.driver_class") String driver,
			@Named("hibernate.connection.username") String username,
			@Named("hibernate.connection.password") String password,
			@Named("hibernate.hbm2ddl.auto") String ddlAuto,
			@Named("hibernate.dialect") String dialect) {
		properties = new Properties();
		properties.put("hibernate.connection.url", url);
		properties.put("hibernate.connection.driver_class", driver);
		properties.put("hibernate.connection.username", username);
		properties.put("hibernate.connection.password", password);
		properties.put("hibernate.hbm2ddl.auto", ddlAuto);
		properties.put("hibernate.dialect", dialect);
	}

	public EntityManager get() {
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa", properties);
		return factory.createEntityManager();
	}
}
